package control.networking;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * This might be a necessary abstraction layer to broadcast messages 
 * from the server to the clients. Instead of the ConnectedChatters 
 * class having a list of ChatClient's and having to request the in/out
 * ports every time, you could store each one in a separate ChatClientHandler
 * that already has the in/out info. Also, would take the functionality of 
 * broadcast() away from ConnectedChatters which would then just call
 * broadcast() on each instance of this class.
 * 
 * ChatServerHandler serves as an interface between the ChatClient and the
 * ChatServer. It handles all of the communication
 *  
 * @author Kevin
 *
 */
public class ChatServerHandler implements Runnable {
	
	private Socket 			clientSock;	// connection to the client
	private BufferedReader 	in;			// Client ---> Server
	private PrintWriter 	out;		// Server ---> Client
	private boolean			finished;
	private ChatServer		masterServer;
	
	
	/**
	 * Initializes a new ChatServerHandler that serves as the interface between
	 * the ChatClient and the ChatServer.
	 * @param socket The socket that is the connection to the client connected.
	 * @param server The master Chat Server.
	 */
	public ChatServerHandler( Socket socket, ChatServer server ) {
		clientSock = socket;
		masterServer = server;
		
				
		try {
			in = new BufferedReader( new InputStreamReader( clientSock.getInputStream() ));
			out = new PrintWriter( clientSock.getOutputStream(), true );
		} catch (IOException e) {
			e.printStackTrace();	}
		
		System.out.println("Created a ChatServerHandler");
	}		

	public void run() {			
		String message;
		finished = false;
		
		while (!finished) {
			try {
				System.out.println("Waiting for Message");
				message = in.readLine();
				processMessage( message );
			} catch(NullPointerException e) {
				System.out.println("message: null");	
			} catch (SocketException e) {
				finished = true;
			} catch (IOException e) {
				e.printStackTrace();	}
		}

		// Done processing and transferring information, now close the connection
		try {
			clientSock.close();
			System.out.println( "Client connection closed\n" );
		} catch (IOException e) {
			e.printStackTrace();	}
	} 
	
	
	/**
	 * 
	 * @param message
	 * @param out
	 */
	private void processMessage( String message ) {
		System.out.println("Processing Message");
		if ( message.trim().toLowerCase().equals("endsession") )
			finished = true;
		if ( message.trim().toLowerCase().equals("chatters") )
			out.println( ConnectedChatters.printChatters() );
		if ( message.trim().toLowerCase().equals("ilovekevin") )
			masterServer.broadcast("I do too. Kevin is the sexiest geek ever. ");
		else{
			masterServer.broadcast( message );
		}
	}
	
	public void displayMessage( String message ) {
		out.println( message );
	}
		
				
}
